<template>
    <div class="contact-add">
        <van-nav-bar title="新增联系人" left-text="返回" left-arrow @click-left="back" />
        <!-- 联系人编辑框 -->
        <van-contact-edit :is-edit="false" show-set-default :contact-info="editingContact" set-default-label="设为默认联系人"
            @save="onSave" :tel-validator="validatorTel" />
    </div>
</template>

<script>
export default {
    props: {

    },
    components: {

    },
    data() {
        return {
            editingContact: {},
        }
    },
    methods: {
        back() {
            this.$router.back()
        },
        onSave(contactInfo) {
            console.log(contactInfo);
            let { name, tel, isDefault } = contactInfo
            if (isDefault) {
                isDefault = 1
            } else {
                isDefault = 0
            }
            console.log(isDefault);
            this.$axios.post(this.$api.contactAdd, { name, tel, isDefault })
                .then(res => res.data)
                .then(res => {
                    if (res.code == 200) {
                        this.$toast.success(res.msg);
                        // 新增成功后返回
                        setTimeout(() => {
                            this.$router.back()
                        }, 1000)
                    } else {
                        this.$toast.fail(res.msg);
                    }
                })

        },
        // 手机号格式校验
        validatorTel(tel) {
            return /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(tel)
        }
    },

}
</script>

<style scoped></style>
